
package dialogs;

import core.COINCOMOComponent;
import database.COINCOMOComponentManager;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import main.GlobalMethods;
import main.Icons;
import panels.ComponentOverviewPanel;

/**
 *
 * @author Raed Shomali
 */
public class ComponentScheduleDriverDialog extends JDialog implements ActionListener
{
    JPopupMenu ratingPopup = new JPopupMenu();

    JPopupMenu incrementPopup = new JPopupMenu();

    JMenuItem vloMenuItem = new JMenuItem( "VLO" );

    JMenuItem loMenuItem = new JMenuItem( "LO" );

    JMenuItem nomMenuItem = new JMenuItem( "NOM" );

    JMenuItem hiMenuItem = new JMenuItem( "HI" );

    JMenuItem vhiMenuItem = new JMenuItem( "VHI" );

    JMenuItem xhiMenuItem = new JMenuItem( "XHI" );

    JMenuItem zeroMenutItem = new JMenuItem( "0%" );

    JMenuItem twentyFiveMenutItem = new JMenuItem( "25%" );

    JMenuItem fiftyMenutItem = new JMenuItem( "50%" );

    JMenuItem seventyFiveMenutItem = new JMenuItem( "75%" );

    JButton ratingButton = new JButton( "NOM" ) ;

    JButton incrementButton = new JButton( "0%" ) ;
    
    JButton applyButton = new JButton( "Apply" );

    JButton resetButton = new JButton( "Reset" );

    JButton closeButton = new JButton( "Close" );
    // The Button From Which the Popup Generated ...
    JButton ownerButton = null;

    COINCOMOComponent component = null ;
    
    public ComponentScheduleDriverDialog( JFrame owner , COINCOMOComponent component )
    {
        super( owner );

        this.setModal( true );

        this.setTitle( "Schedule" );

        this.component = component ;
        
        GlobalMethods.updateStatusBar( "Done." );

        // Popup
        ratingPopup.add( vloMenuItem );
        ratingPopup.add( loMenuItem );
        ratingPopup.add( nomMenuItem );
        ratingPopup.add( hiMenuItem );
        ratingPopup.add( vhiMenuItem );
        ratingPopup.add( xhiMenuItem );

        incrementPopup.add( zeroMenutItem );
        incrementPopup.add( twentyFiveMenutItem );
        incrementPopup.add( fiftyMenutItem );
        incrementPopup.add( seventyFiveMenutItem );

        // Menu Items
        vloMenuItem.addActionListener( this );
        loMenuItem.addActionListener( this );
        nomMenuItem.addActionListener( this );
        hiMenuItem.addActionListener( this );
        vhiMenuItem.addActionListener( this );
        xhiMenuItem.addActionListener( this );

        zeroMenutItem.addActionListener( this );
        twentyFiveMenutItem.addActionListener( this );
        fiftyMenutItem.addActionListener( this );
        seventyFiveMenutItem.addActionListener( this );

        // Buttons
        applyButton.addActionListener( this );
        resetButton.addActionListener( this );
        closeButton.addActionListener( this );

        applyButton.setFocusable( false );
        resetButton.setFocusable( false );
        closeButton.setFocusable( false );

        applyButton.setIcon( Icons.SAVE_ICON );
        resetButton.setIcon( Icons.RESET_ICON );
        closeButton.setIcon( Icons.CLOSE_ICON );

        // GUI
        this.setLayout( null );

        JLabel ratingLabel = new JLabel( "Rating" );
        JLabel incrementLabel = new JLabel( "Increment" );

        ratingLabel.setFont( new Font( "arial" , 1 , 11 ) );
        incrementLabel.setFont( new Font( "arial" , 1 , 11 ) );

        ratingLabel.setHorizontalAlignment( JLabel.CENTER );
        incrementLabel.setHorizontalAlignment( JLabel.CENTER );

        this.add( ratingLabel );
        this.add( incrementLabel );

        ratingLabel.setBounds( 165 , 20 , 65 , 25 );
        incrementLabel.setBounds( 240 , 20 , 65 , 25 );

        JLabel label1 = new JLabel( "Schedule ............" );

        label1.setBounds( 40 , 50 , 200 , 25 );

        label1.setFont( new Font( "arial" , 1 , 11 ) );

        this.add( label1 );

        ratingButton.setText( GlobalMethods.convertToRating( component.getScedBase() ) ) ;
        incrementButton.setText( GlobalMethods.convertToPercent( component.getScedIncr() ) ) ;
        
        ratingButton.setFont( new Font( "courier" , 1 , 16 ) );
        incrementButton.setFont( new Font( "courier" , 1 , 16 ) );

        ratingButton.setFocusable( false );
        incrementButton.setFocusable( false );

        ratingButton.addActionListener( this );
        incrementButton.addActionListener( this );

        this.add( ratingButton );
        this.add( incrementButton );

        ratingButton.setBounds( 165 , 50 , 65 , 25 );
        incrementButton.setBounds( 240 , 50 , 65 , 25 );

        // Panel
        JPanel southPanel = new JPanel();
        southPanel.add( applyButton );
        southPanel.add( resetButton );
        southPanel.add( closeButton );

        this.add( southPanel );

        southPanel.setBounds( 0 , 120 , 340 , 40 );

        this.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
        this.setLocation( this.getOwner().getX() + 100 , this.getOwner().getY() + 100 );
        this.setResizable( false );
        this.setSize( 340 , 200 );
        this.setVisible( true );
    }

    public void actionPerformed( ActionEvent e )
    {
        if ( e.getSource() == applyButton )
        {
            applyButton.setEnabled( false ) ;
            resetButton.setEnabled( false ) ;
            closeButton.setEnabled( false ) ;
            
            GlobalMethods.updateStatusBar( "Saving ..." ) ;
            
            SwingUtilities.invokeLater( new Runnable()
            {
                public void run()
                {
                    component.setScedBase( GlobalMethods.convertFromRating( ratingButton.getText() ) ) ;
                    component.setScedIncr( GlobalMethods.convertFromPercent( incrementButton.getText() ) ) ;
                    
                    // Update Database ..
                    COINCOMOComponentManager.updateComponent( component ) ;
                    
                    // Update Summary Report
                    ComponentOverviewPanel.updateEstimationTextPane( true );
                    
                    GlobalMethods.updateStatusBar( "Scale Factors Saved." ) ;
                    
                    // Back To Default
                    applyButton.setEnabled( true ) ;
                    resetButton.setEnabled( true ) ;
                    closeButton.setEnabled( true ) ;
                }
            });
        }
        else if ( e.getSource() == resetButton )
        {
            ratingButton.setText( "NOM" );
            incrementButton.setText( "0%" );
        }
        else if ( e.getSource() == closeButton )
        {
            // Free Resources ... Close Window
            this.dispose();
        }
        else if ( e.getSource() == vloMenuItem || e.getSource() == loMenuItem || e.getSource() == nomMenuItem ||
                e.getSource() == hiMenuItem || e.getSource() == vhiMenuItem || e.getSource() == xhiMenuItem )
        {
            ownerButton.setText( ((JMenuItem) e.getSource()).getText() );
        }
        else if ( e.getSource() == zeroMenutItem || e.getSource() == twentyFiveMenutItem ||
                e.getSource() == fiftyMenutItem || e.getSource() == seventyFiveMenutItem )
        {
            ownerButton.setText( ((JMenuItem) e.getSource()).getText() );
        }
        else
        {
            ownerButton = ((JButton) e.getSource());

            if ( ownerButton.getText().endsWith( "%" ) )
            {
                incrementPopup.show( (Component) e.getSource() , ((JButton) e.getSource()).getWidth() / 2 , ((JButton) e.getSource()).getHeight() / 2 );
            }
            else
            {
                ratingPopup.show( (Component) e.getSource() , ((JButton) e.getSource()).getWidth() / 2 , ((JButton) e.getSource()).getHeight() / 2 );
            }
        }
    }

}
